#!/bin/bash
#
#  Copyright (c) 2022, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#

contains()
{
    local search_term=$1
    shift 1
    IFS=" " read -r -a list <<<"$@"
    echo "${list[@]}" | grep -w -q -E "$search_term"
}

create_srec()
{
    local executable_flag

    if [ "$(uname)" = 'Linux' ]; then
        executable_flag=(-executable)
    elif [ "$(uname)" = 'Darwin' ]; then
        executable_flag=(-perm +111)
    fi

    find "$1" -type f "${executable_flag[@]}" -not -name '*.*' -exec arm-none-eabi-objcopy -O srec {} {}.s37 \; -exec ls -alh {} {}.s37 \;
}

die()
{
    echo " ** ERROR: $1"
    exit 1
}

parse_configuration()
{
    local extension_dir="${extension_dir-.}"
    local config_yml=""

    if [ $# -eq 0 ]; then
        # No args passed. Use defaults
        config_yml="${extension_dir}/config.yml"
    elif [ $# -eq 1 ]; then
        extension_dir=$1
        config_yml="${extension_dir}/config.yml"
    elif [ $# -eq 2 ]; then
        extension_dir=$1
        config_yml=$2
    else
        echo "ERROR: Too many args"
        echo "usage: parse_configuration [extension_dir] [config_yml]"
        return 1
    fi

    # Check that ${config_yml} exists
    if [ ! -f "${config_yml}" ]; then
        echo "ERROR: '${config_yml}' not found"
        return 2
    fi

    # Temporarily disable 'set -e'
    # This will allow configuration files that only define a subset of "*_slcp" keys
    old_setting=${-//[^e]/}
    set +e
    # grep raw values from the configuration file
    line="$(grep 'rcp_uart_slcp:' "${config_yml}")"
    rcp_uart_slcp="${line//rcp_uart_slcp:/}"
    line="$(grep 'rcp_spi_slcp:' "${config_yml}")"
    rcp_spi_slcp="${line//rcp_spi_slcp:/}"
    line="$(grep 'soc_slcp:' "${config_yml}")"
    soc_slcp="${line//soc_slcp:/}"
    line="$(grep 'board:' "${config_yml}")"
    board="${line//board:/}"

    # Strip spaces
    rcp_uart_slcp="${rcp_uart_slcp// /}"
    rcp_spi_slcp="${rcp_spi_slcp// /}"
    soc_slcp="${soc_slcp// /}"
    board="${board// /}"

    # Strip quotes
    rcp_uart_slcp=$(eval echo "${rcp_uart_slcp}")
    rcp_spi_slcp=$(eval echo "${rcp_spi_slcp}")
    soc_slcp=$(eval echo "${soc_slcp}")
    board=$(eval echo "${board}")

    # Make paths relative to extension_dir, stripping leading './'
    rcp_uart_slcp="${extension_dir}/${rcp_uart_slcp#./}"
    rcp_spi_slcp="${extension_dir}/${rcp_spi_slcp#./}"
    soc_slcp="${extension_dir}/${soc_slcp#./}"
    if [[ -n $old_setting ]]; then set -e; else set +e; fi

}
